package com.sinopec.android;

import java.util.ArrayList;
import java.util.HashMap;

import com.sinopec.android.adapter.AddressBookListAdapter;
import com.sinopec.android.bean.AddressBean;
import com.sinopec.android.bean.AddressBook;
import com.sinopec.android.bean.ResultData;
import com.sinopec.android.bean.ThreadCallBack;
import com.sinopec.android.bean.UserInfo;
import com.sinopec.android.constant.ConfigAttrs;
import com.sinopec.android.net.ThreadManger;
import com.sinopec.android.util.SharePreferencesUtil;
import com.sinopec.android.util.StringUtil;
import com.sinopec.android.util.ToastUtil;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
* @ClassName: OnlineAddressBookActivity
* @Description: TODO(在线通讯录)
* @author liuwei
* @date 2012-3-27 下午02:36:10
* 
*/
public class OnlineAddressBookActivity extends BaseActivity implements OnClickListener,ThreadCallBack{
	private ListView listView;
	private TextView no_result_text_view;
	private EditText keywordEdit;
	private Button queryBtn;
	private String keywordValue;
	public  ArrayList<AddressBook>  listData;
	AddressBookListAdapter bookListAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.online_addressbook_view);
		initView();
	}
	
	void initView(){
		listView = (ListView) findViewById(R.id.list);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,   
                    int position, long id) {
				  if(listData.get(position)!=null){
					 String tel = listData.get(position).getTel();
					 if(StringUtil.isNotEmpty(tel)){
						 Intent intent = new Intent(Intent.ACTION_VIEW);
						 intent.setData(Uri.parse("tel:"+tel));
						 startActivity(intent);          
					 }
				  }
				  
				
			}
		});
		no_result_text_view = (TextView) findViewById(R.id.no_result_text_view);
		keywordEdit = (EditText) findViewById(R.id.keyword);
		queryBtn = (Button) findViewById(R.id.query_btn);
		queryBtn.setOnClickListener(this);
		keywordValue = keywordEdit.getText().toString().trim();
		sendToServer();

	}
	@Override
	public void onCallbackFromThread(ResultData resultData) {
		// TODO Auto-generated method stub
		if(resultData!=null && resultData.getResultList()!=null){
		}else{
			Toast.makeText(this,R.string.get_fail, 0).show();
			return;
		}
		if(resultData.getResultList().get(0) instanceof String){
			Toast.makeText(this,resultData.getResultList().get(0).toString(), 0).show();
			return ;
		}
		ArrayList arrayList = resultData.getResultList();
		//查询通讯录结果
		if(arrayList.size()>0&&arrayList.get(0) instanceof AddressBean){
			AddressBean addressBean = (AddressBean) arrayList.get(0);
			if(addressBean!=null){
				listData = addressBean.getAddressBookList();
				if(listData==null||(listData!=null&&listData.isEmpty())){
					listView.setVisibility(View.GONE);
					no_result_text_view.setVisibility(View.VISIBLE);
				}else{
					listView.setVisibility(View.VISIBLE);
					no_result_text_view.setVisibility(View.GONE);
					if(bookListAdapter!=null){
						bookListAdapter.listData = listData;
						bookListAdapter.notifyDataSetChanged();
					}else{
						bookListAdapter = new AddressBookListAdapter(this,listData);
						listView.setAdapter(bookListAdapter);
					}
				}
			}
			
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.query_btn:
			//1校验关键字
			keywordValue = keywordEdit.getText().toString().trim();
//			if(StringUtil.isEmpty(keywordValue)){
//				ToastUtil.showToast(getApplicationContext(), "请输入要查找的关键字");
//				return;
//			}
			sendToServer();
			break;

		default:
			break;
		}
	}
	
	/**
	 * @Title :sendToServer
	 * @Description :TODO 调用查询通讯录接口
	 * @params 
	 * @return void
	 * @throws
	 * 
	 */
	void  sendToServer(){
		String reqUrl =ConfigAttrs.BASE_URL+"GetAddressBookList.ashx";
		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("userName", SharePreferencesUtil.getLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_ACCOUNT));
		hashMap.put("passWord", SharePreferencesUtil.getLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_PWD));
		if(StringUtil.isEmpty(deviceId)){
			deviceId = "0000000000";
		}
		hashMap.put("deviceId", deviceId);
		hashMap.put("name", keywordValue);

		ThreadManger.exeTask(this, ConfigAttrs.QUERY_ADDRESSBOOK_TASK_FLAG, hashMap, reqUrl);
	}
}
